<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$this->model('model')->checkauth($merchid);
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$tabs=array(
	array('op'=>'list','title'=>'平台商品列表'),
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架商品
//上架商品
if($_RQ['op']=='list'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
	}
	$params=array('status'=>1,'pluginname'=>'','onepluginname'=>'');
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid']=0;
	$params['status']=isset($_RQ['status'])?intval($_RQ['status']):1;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
	}elseif(!empty($_RQ['page'])){
		if($this->session('pcate')){
			$params['pcate']=$this->session('pcate');
			$return['params']['pcate']=$this->session('pcate');
		}
	}
	if(trim($postdata['keyword'])){
		$params['title like']='%'.trim($postdata['keyword']).'%';
		$return['params']['keyword']=trim($postdata['keyword']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','uniacid','nochecked','paixu','title','image','pcate','ccate','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
				$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate']),'title');
				$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
				$item['salenum']=$item['salenum']?$item['salenum']:0;
				$check=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'api_id'=>$item['id'],'api_pluginname'=>'jy_merch'));
				if($check['id']){
					$item['isadd']=1;
				}else{
					$item['isadd']=0;
				}
				$item['buyprice']=model('jy_weishop/goods')->get_diyprice($item);
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：下面明明有商品，但是<red>手机端没有商品</red>显示?很可能本商户下面添加了分类，但是这些分类里面没有商品！','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'img_string','title'=>'商品名称','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				//array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('type'=>'img_string','title'=>'商品分类',
			'data'=>array(
				array('title'=>'一级：','name'=>'pcatetitle'),
				array('title'=>'二级：','name'=>'ccatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量',
			'data'=>array(
				array('title'=>'库存：','name'=>'total'),
				array('title'=>'销量：','name'=>'salenum','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'销售价格','type'=>'string'),
			array('name'=>'buyprice','title'=>'会员价','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'isadd','title'=>'已采集？','type'=>'radio','options'=>'isadd'),
			array('name'=>'nochecked','title'=>'审核状态','type'=>'radio','options'=>'nochecked'),
			array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletypes'),
		),'batch'=>array(
			array('type'=>'modal','url'=>'//daixiao/','title'=>'一键采集'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'商品名称'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'商品分类','options'=>'groups'),
		)
	);
	$return['groups']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'isshow'=>1,'parentid'=>0));
	if(empty($return['groups'])){
		$return['groups']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0));
	}
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
	$return['nochecked']=array('审核通过','未审核');
	$return['isadd']=array('未采集','已采集');
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/good_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='daixiao'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$goodsids = $postdata['ids'];
		foreach($goodsids as $goodsid){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
			if(!$goods['id']){
				continue;
			}
			$isadd=true;
			$buyprice=model('jy_weishop/goods')->get_diyprice($goods);
			if($postdata['addprice']){
				if(substr($postdata['addprice'],-1)=='%'){
					$addprice=($buyprice*substr($postdata['addprice'],0,-1))/100;
				}else{
					$addprice=$postdata['addprice'];
				}
			}
			$data=array(
				'price'=>$buyprice+$addprice,
				'costprice'=>$buyprice,
			);
			//检查是否已添加该商品
			$check=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'api_id'=>$goodsid,'api_pluginname'=>'jy_merch'));
			if($check){
				pdo_update('jy_weishop_goods',$data,array('merchid'=>$merchid,'api_id'=>$goodsid,'api_pluginname'=>'jy_merch'));
			}else{
				unset($goods['id']);
				$goods['price']=$data['price'];
				$goods['costprice']=$data['costprice'];
				$goods['merchid']=$merchid;
				$goods['pluginname']='jy_merch';
				$goods['onepluginname']='jy_merch';
				$goods['api_id']=$goodsid;
				$goods['api_pluginname']='jy_merch';
				pdo_insert('jy_weishop_goods',$goods);
				$newgoodsid=pdo_insertid();
				$specs=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
				if(count($specs)){
					foreach($specs as $sk=>&$sv){
						$options=array();
						$spec_item=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sv['id'],'goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
						unset($sv['id']);
						$sv['goodsid']=$newgoodsid;
						pdo_insert('jy_weishop_goods_spec',$sv);
						$sv['newid']=pdo_insertid();
						if(count($spec_item)){
							foreach($spec_item as $ok=>$ov){
								$spec_item=$ov;
								unset($ov['id']);
								$ov['goodsid']=$newgoodsid;
								$ov['specid']=$sv['newid'];
								pdo_insert('jy_weishop_goods_spec_item',$ov);
								$spec_item['newid']=pdo_insertid();
								$options[]=$spec_item;
							}
						}
						$sv['options']=$options;
					}
				}
				$option=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
				if(count($option)){
					foreach($option as $ov){
						$oldspecs=explode('_',$ov['specs']);
						$newspecs=array();
						foreach($specs as $sk=>$sp){
								foreach($sp['options'] as $op){
									if($oldspecs[$sk] == $op['id']){
										$newspecs[]=$op['newid'];
										break;
									}
								}
						}
						$ov['specs']=implode('_',$newspecs);
						$ov['goodsid']=$newgoodsid;
						unset($ov['id']);
						pdo_insert('jy_weishop_goods_option',$ov);
					}
				}
			}
		}
		if(!$isadd){
			exi('请先选择要采集的商品！','error');
		}
		exi('批量采集成功！','','/manage.good_jinlist/');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	$item['ids']=$ids;
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'采集商品',
		'edit'=>array(
			array('type'=>'string','name'=>'addprice','title'=>'加价','help'=>'填数字，直接加固定金额，加%，表示按比例加价！比如填10%，表示加价10%'),
		),
	);
	exi($return);
}